package com.hospital.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import com.hospital.pojo.Ghxxb;
import com.hospital.pojo.Ksb;
import com.hospital.pojo.Ysb;
import com.hospital.service.HospInitQueryService;
import com.hospital.vo.BaseQueryVo;
import com.hospital.vo.GuahaoResultVo;
import com.hospital.vo.ResultVO;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class HospInitQuery {
    @Resource
    private HospInitQueryService hospInitQueryService;

    @InitBinder
    protected void initDateFormatBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @RequestMapping(value = "/guahao/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<GuahaoResultVo> guahaoInit(Integer pageNumInput, BaseQueryVo queryVo) {
//        System.out.println(pageNumInput);
//        System.out.println(queryVo);
        PageInfo<GuahaoResultVo> guahaoInfo = hospInitQueryService.getGuahaoInfo(pageNumInput, 5, queryVo);
        ResultVO<GuahaoResultVo> resultVO = new ResultVO<>();
        resultVO.setPageInfo(guahaoInfo);
//        System.out.println(resultVO);
        return resultVO;
    }

    @RequestMapping(value = "/guahaodetail/{blh}", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<Ghxxb> getGuahaoDetail(@PathVariable("blh") Integer blh) {
        System.out.println(blh);
        Ghxxb ghxxbByBlh = hospInitQueryService.getGhxxbByBlh(blh);
        System.out.println(ghxxbByBlh);
        ResultVO<Ghxxb> resultVO = new ResultVO<>();
        resultVO.setObj(ghxxbByBlh);
        return resultVO;
    }

    /**
     * 获取全部的科室以供用户选择
     *
     * @return
     */
    @RequestMapping(value = "/keshilist", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<Ksb> getKeshiDetail() {
        System.out.println("kslist");
        List<Ksb> keshiInfo = hospInitQueryService.getKeshiInfo();
        ResultVO<Ksb> ksbResultVO = new ResultVO<>();
        ksbResultVO.setObjList(keshiInfo);
        return ksbResultVO;
    }

    /**
     * 获取医生信息以供用户选择
     */
    @RequestMapping(value = "/doclist/{ksId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<Ysb> getYsbInfo(@PathVariable("ksId") Integer ksId) {
        List<Ysb> docInfo = hospInitQueryService.getDocInfo(ksId);
        ResultVO<Ysb> ysbResultVO = new ResultVO<>();
        ysbResultVO.setObjList(docInfo);
        return ysbResultVO;
    }

    /**
     * 获取医生信息以供用户选择
     */
    @RequestMapping(value = "/doclist", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO<Ysb> getYsbInfo() {
        System.out.println("doclist");
        List<Ysb> docInfo = hospInitQueryService.getDocInfo();
        ResultVO<Ysb> ysbResultVO = new ResultVO<>();
        ysbResultVO.setObjList(docInfo);
        return ysbResultVO;
    }

    /**
     * 退号
     */
    @RequestMapping(value = "/tuihao", method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Object> tuiHao(String blh) {
        int tuiHao = hospInitQueryService.tuiHao(Integer.parseInt(blh));
        ResultVO<Object> vo = new ResultVO<>();
        if (tuiHao == 1) {
            return vo;
        }
        vo.setCode(500);
        vo.setMessage("退号失败，请联系管理员或重试！");
        return vo;
    }

    /**
     * g更新挂号信息
     * @param ghxxb
     * @return
     */
    @RequestMapping(value = "/updateGhxx", method = RequestMethod.PUT)
    @ResponseBody
    public ResultVO<Object> updateGhxx(Ghxxb ghxxb) {
        System.out.println("ghxxb," + ghxxb);
        int i = hospInitQueryService.updateGhxxb(ghxxb);
        ResultVO<Object> objectResultVO = new ResultVO<>();
        if (i != 1) {
            objectResultVO.setCode(500);
            objectResultVO.setMessage("挂号信息更新失败，请重试或联系管理员");
        }
        return objectResultVO;
    }

    /**
     * 批量退号
     * @param data
     * @return
     */
    @RequestMapping(value = "/multiTuihao", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO<Object> multiTuihao(@RequestBody Map<String, ArrayList<Integer>> data){
//    public ResultVO<Object> multiTuihao(String blhs){
        System.out.println(data);
        List blhs = (ArrayList) data.get("blhs");
        System.out.println(blhs);
        ResultVO<Object> vo = new ResultVO<>();
        for (int i = 0; i < blhs.size(); i++) {
            int tuiHao = hospInitQueryService.tuiHao((Integer) blhs.get(i));
            if (tuiHao != 1){
                vo.setCode(500);
                vo.setMessage("批量退号失败！");
                return vo;
            }
        }
        vo.setMessage("批量退号成功");
        return vo;
    }

    @RequestMapping(value = "/addGhxx", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO<Object> addGhxx(Ghxxb ghxxb){
        System.out.println("add:" + ghxxb);
        ghxxb.setZjlx("SFZ");
        ghxxb.setGhsj(new Date());
        ghxxb.setZt(1);
        int i = hospInitQueryService.addGhxx(ghxxb);
        ResultVO<Object> vo = new ResultVO<>();
        if (i != 1){
            vo.setCode(500);
            vo.setMessage("新增挂号信息失败，请重试或联系管理员");
        }
        return vo;
    }
}
